package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.util.Log;
import com.example.scaler.AclasScaler;
import com.zbw.weightlibrary.Connector.BaseControlImpl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.WeightData;

/**
 * 作者：lxkang3 on 2022/5/25 13:44
 * 这个类是干嘛用的？
 * 顶尖串口秤一体秤
 */
public class DingJianSerialPort extends BaseControlImpl {
    private String tag = "DingJianSerialPort";
    private final IinfoCallback mCallback;
    private final String mPort; //串口电子称端口port
    private final int mBaudrate; //串口电子称波特率baudrate ：：demo  9600
    private boolean ReadWeight;//是否读取重量
    private final Context context;
    private AclasScaler m_scaler = null;


    public DingJianSerialPort(Context context, IinfoCallback callback, WeightData data) {
        super(callback);
        this.mCallback = callback;
        this.mPort = "/dev/" + data.getPort();
        this.mBaudrate = data.getBaudrate();
        this.context = context;
    }

    @Override
    public void bleConnect() {
        if (mPort == null) {
            mCallback.onConnect("请选择连接的串口", false);
        } else {
            Log.d(tag,"AclasConnect----------------");
            InitDevice();
        }
    }

    @Override
    public void bleDisconnect() {
        if (m_scaler != null) {
            Log.d(tag,"CloseScale----------------");
            m_scaler.AclasDisconnect();
            mCallback.onDisconnect("断开连接", true);
        }
    }

    @Override
    public void getWeight() {
        ReadWeight = true;
    }

    /**
     * 停止读取重量
     */
    @Override
    public void stopWeight() {
        ReadWeight = false;
    }

    @Override
    public void clearTare() {
        if (m_scaler != null) {
            boolean bFlag = false;
            bFlag = m_scaler.AclasReset();
            if (bFlag) {
                Log.d(tag,"clearTare----------------Success");
            }else {
                Log.d(tag,"clearTare----------------Fail");
            }
        }
    }

    /**
     * 去皮
     */
    @Override
    public void tare() {
        if (m_scaler != null) {
            boolean bFlag = false;
            bFlag = m_scaler.AclasTare();
            if (bFlag) {
                Log.d(tag,"clearTare----------------Success");
            }else {
                Log.d(tag,"clearTare----------------Fail");
            }
        }
    }

    /**
     * 重置清零
     */
    @Override
    public void zero() {
        if (m_scaler != null) {
            boolean bFlag = false;
            bFlag = m_scaler.AclasZero();
            if (bFlag) {
                Log.d(tag,"zero----------------Success");
            }else {
                Log.d(tag,"zero----------------Fail");
            }
        }
    }

    /**
     * 连接初始化
     */
    private void InitDevice() {
        if (m_scaler != null && m_scaler.AclasIsConnect()) {
            m_scaler.AclasDisconnect();
            Log.d(tag,"初始化连接---------AclasDisconnect");
        }
        try {
            m_scaler = new AclasScaler(AclasScaler.Type_SerialPort, context, m_listener);
            m_scaler.AclasConnect(mPort, mBaudrate, 0);
            if (m_scaler.AclasIsConnect()) {
                m_scaler.AclasReset();
                mCallback.onConnect("连接成功", true);
                Log.d(tag,"AclasConnect------Success");
            }else {
                mCallback.onConnect("连接失败", false);
                Log.d(tag,"AclasConnect------Fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mCallback.onConnect("连接失败", false);
            Log.d(tag,"AclasConnect------Error");
        }
    }

    private AclasScaler.AclasScalerListener m_listener = new AclasScaler.AclasScalerListener() {

        @Override
        public void onError(int errornum, String str) {
            // TODO Auto-generated method stub
            String info = "onError: "+errornum+" str:"+str;
            Log.d(tag,info);
        }

        @Override
        public void onDisConnected() {
            Log.d(tag,"AclasConnect------onDisConnected");
        }

        @Override
        public void onConnected() {
            Log.d(tag,"AclasConnect------onConnected");
        }

        public void onRcvData(AclasScaler.WeightInfoNew info) {
            Log.d(tag,"AclasConnect------onRcvData"+info.netWeight);
            if (ReadWeight) {
                String value = String.valueOf(info.netWeight);
                mCallback.onGetWeight(value, info.isStable);
            }
        }

        public void onUpdateProcess(int iIndex,int iTotal){
        }
    };
}
